package cn.jack.audioclip

import android.content.Context
import android.content.res.AssetFileDescriptor
import android.os.Bundle
import android.os.Environment
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import cn.jack.audioclip.ui.theme.AudioClipTheme
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AudioClipTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting(modifier = Modifier.clickable {
                        audioClip(this)
                    })
                }
            }
        }

        requsetReadImeiInfo(this)
    }
}

fun audioClip(context: Context) {
    Thread {
        val aacPath = File(Environment.getExternalStorageDirectory(), "audioSource.mp3").absolutePath
        val outPath = File(Environment.getExternalStorageDirectory(), "output5.mp3").absolutePath
        try {
            copyAssets(context, "audioSource.mp3", aacPath)
            ClipHelper.clip(aacPath, outPath, 10 * 1000 * 1000, 15 * 1000 * 1000)
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }.start()
}

private fun copyAssets(context: Context, assetsName: String, path: String) {
    val assetFileDescriptor: AssetFileDescriptor = context.assets.openFd(assetsName)
    val from = FileInputStream(assetFileDescriptor.fileDescriptor).channel
    val to = FileOutputStream(path).channel
    from.transferTo(assetFileDescriptor.startOffset, assetFileDescriptor.length, to)
}

private fun requsetReadImeiInfo(context: Context) {
    XXPermissions
        .with(context)
        .permission(Permission.Group.STORAGE)
        .request(object : OnPermissionCallback {
            override fun onGranted(
                permissions: List<String>,
                allGranted: Boolean
            ) {
                if (!allGranted) {
                    Toast.makeText(
                        context,
                        "获取部分权限成功，但部分权限未正常授予",
                        Toast.LENGTH_SHORT
                    ).show()
                    return
                }
            }

            override fun onDenied(
                permissions: List<String>,
                doNotAskAgain: Boolean
            ) {
                if (doNotAskAgain) {
                    // 如果是被永久拒绝就跳转到应用权限系统设置页面
                    XXPermissions.startPermissionActivity(context, permissions)
                } else {
                    Toast.makeText(context, "获取文件读取权限失败", Toast.LENGTH_SHORT).show()
                }
            }
        })
}

@Composable
fun Greeting(modifier: Modifier = Modifier) {
    Text(
        text = "点击一下开始进行音频剪辑",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    AudioClipTheme {
        Greeting()
    }
}